package com.example.yyh.recorddemo.manager;

import android.content.Context;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;

import com.example.yyh.recorddemo.utils.FileUtils;

import java.io.File;

/**
 * Created by yaoyihang on 2018/11/30.
 * 录音工具类
 */

public class RecordManager {


    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    Context context;
    /*-------------------------------------创建单例对象------------------------------------------*/

    private static RecordManager INSTANCE;

    private RecordManager() {
        //创建文件夹
        FileUtils.makeDirectory(music);

        if (INSTANCE != null) {
            throw new RuntimeException("单例漏洞");//防止单例反射
        }
    }

    public static RecordManager getInstance() {
        if (INSTANCE == null) {
            synchronized (RecordManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new RecordManager();
                }
            }
        }
        return INSTANCE;
    }

    /*-------------------------------------主要代码------------------------------------------*/

    private final String APP_DIRECTORY = "record_demo";//项目的主文件

    private final String SDCARD = Environment.getExternalStorageDirectory().toString();//sd卡路径

    private final String ROOT = SDCARD + File.separator + APP_DIRECTORY + File.separator;//创建的文件路径

    private final String music = ROOT + "music" + File.separator;//存音频的文件地址

    private MediaRecorder mMediaRecorder;//我就用它录制了哈，另外还有一个兄弟AudioRecord,在这里用不合适

    private boolean isRecording;//是否录制中

    private File file;//音频文件

    private int mTotalCount;//数据库数据总数


    /**
     * 开始录制
     */
    public void startRecord() {

        mTotalCount = RecordDbManager.getInstance().getTotalCount();
        Log.e("debug", "totalCount--->" + mTotalCount);

        File filesDir = context.getFilesDir();
        //filesDir.
        Log.i("filesDir", "startRecord: "+filesDir);

        //String path = music + "录音_" + System.currentTimeMillis() + ".amr";
        String path = filesDir + "录音_" + System.currentTimeMillis() + ".amr";
        Log.i("path", "startRecord: "+path);
        file = new File(path);
        boolean orExistsFile = FileUtils.createOrExistsFile(file);
        //if()

        //android 创建文件

        //Context.getFilesDir()



        mMediaRecorder = new MediaRecorder();
        //设置录音的声音来源
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        //设置录制的声音的输出格式
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //设置声音的编码格式
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        //设置采样率
        mMediaRecorder.setAudioSamplingRate(44100);
        //设置编码率
        mMediaRecorder.setAudioEncodingBitRate(5 * 1024 * 1024);
        //设置输出文件
        mMediaRecorder.setOutputFile(file.getAbsolutePath());

        isRecording = true;
        try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止录制
     */
    public File stopRecord() {
        isRecording = false;
        releaseRecord();
        return file;
    }

    /**
     * 获取录制状态
     */

    public boolean isRecord() {
        return isRecording;
    }


    /**
     * 退出当前页面释放资源
     */
    public void releaseRecord() {
        //java.lang.IllegalStateException  MediaRecorder
        mMediaRecorder.stop();
        mMediaRecorder.release();
        mMediaRecorder = null;
    }

}
